<?php
/*
 * Created on 24 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
 
 
 class LangueDAO extends DAO{
 	private $Bean;

	function getById($id){
		//return a Langue with a given id
		try
		{
				$query="select * from langue where id=$id and disponible=true;";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					
					$this->Bean = new Langue((int)$Bean->id);
					
					$this->Bean->setAbr($Bean->abr);
				}
		}
		catch(Exception $e)	{
			throw $e;
			}
		
		return $this->Bean;
	}
	function remove($Bean){
		try{
			$query="update langue set disponible=false where id='".$Bean->getId()."';";
			$this->pdo->exec($query);
			$bool=true;	
		}catch(Exception $e)	{
			$bool=false;
			//$this->pdo->rollBack();
			}
		return $bool;
			
	}
	function getByAbr($abr){
		//return a Langue with a given abr
		try
		{
				$query="select * from langue where abr=$abr and disponible=true;";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					$this->Bean = $this->getById((int)$Bean->id);
				}
		}
		catch(Exception $e)	{$this->pdo->message;}
		
		return $this->Bean;
	}
	function update($Bean){
		//update a given Langue
		try{
			$query = "update langue set abr='".$Bean->getAbr()."' where id=".$Bean->getId().";";
			$this->pdo->exec($query);
			$bool=true;	
		}
		catch(Exception $e)	{
			$bool=false;
			$this->pdo->rollBack();
			}
		return $bool;
	}
	function add($abr){
		// insert and return a new Langue
		try{
			$query="select * from langue where abr='$abr';";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			if(count($array_obj)>0){
				$this->Bean = $this->getById((int)$array_obj[0]->id);
				$updt = "update langue set disponible=true where abr=$abr";
				$this->pdo->exec($updt);
			}else{
				$insertQuery = "insert into langue (id,abr) values (null,'$abr');";
				$this->pdo->exec($insertQuery);
				$this->Bean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)	{$this->pdo->message;}
		return $this->Bean;
	}
	function All(){
		try
		{
				$BeanArray = array();
				$query="select * from langue where disponible=true;";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				foreach($array_obj as $Bean){
					$this->Bean = $this->getById((int)$Bean->id);
					array_push($BeanArray,$this->Bean);
				}
		}
		catch(Exception $e)	{
			throw $e;
		}
		
		return $BeanArray;
		
	}
 }
?>
